﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using LView.CMS.IServices;
using LView.CMS.Core;
using LView.CMS.Models;
using LView.CMS.ViewModels;
using LView.CMS.Core.Helper;
using Aspose.Slides;
using System.Drawing;
using System.Drawing.Imaging;

namespace LView.CMS.Site.Controllers
{
    public class MyTaskController : BaseController
    {
        private readonly IMyTaskService _myTaskService;
        public MyTaskController(IMyTaskService myTaskService)
        {
            _myTaskService = myTaskService;
        }
        public IActionResult Index()
        {
            var (myTaskList, myTaskCount) = _myTaskService.GetMyTaskList(CurrentUser.UserId);
            return View(myTaskList);
        }

        public IActionResult TimeLine()
        {
            var (myTaskList, myTaskCount) = _myTaskService.GetMyTaskList(CurrentUser.UserId);
            return View(myTaskList);
        }

        public IActionResult MyTaskDetail(string taskId, string courseId)
        {
            var taskInfo = MongodbHelper.Instance.FindEntity<LTask>(x => x.Id == taskId);
            var (processInfo, processCount) = MongodbHelper.Instance.FindList<LProcess>(x => x.TaskId == taskId && x.UserId == CurrentUser.UserId);
            var hasStudyPeriod = 0; var hasGotScore = 0;
            processInfo.ForEach(x => { hasStudyPeriod += x.CourseGotPeriod; hasGotScore += x.CourseGotScore; });
            ViewData["hasStudyPeriod"] = hasStudyPeriod;
            ViewData["hasGotScore"] = hasGotScore;
            return View(taskInfo);
        }

        public IActionResult ShowOtherDetail(string courseFileId, string courseId, string taskId, string courseRequiredTime)
        {
            if (courseFileId != null)
            {
                var task = MongodbHelper.Instance.FindEntity<LTask>(x => x.Id == taskId);
                var courseProcess = MongodbHelper.Instance.FindEntity<LProcess>(x => x.CourseId == courseId);
                var courseFile = MongodbHelper.Instance.FindEntity<LFile>(x => x.Id == courseFileId);
                var coursePath = courseFile.Path;
                var requiredPeriod = 0;
                task.Category.ForEach(x =>
                {
                    x.Course.ForEach(y =>
                    {
                        if (y.CourseId == courseId)
                        {
                            requiredPeriod = y.CoursePeriod;
                        }
                    });
                });
                var hasGotPeriod = courseProcess.CourseGotPeriod;
                ViewData["RemainTime"] = requiredPeriod * 60 - hasGotPeriod;
                //var dir = @"C:\Users\Gerald\Desktop\EEEEE\";
                var file = new System.IO.FileInfo(coursePath);
                var dir = file.Directory;
                var filenameArr = file.Name.Split('.');
                var picList = new List<string>();
                //var fileNameNoSuffix = "ICT SPC&Commonality Implementation Report_2020W19(1)";
                //var fileNameWithSuffix = "ICT SPC&Commonality Implementation Report_2020W19(1).pptx";
                using (Presentation pres = new Presentation(coursePath))
                {
                    var w = (int)pres.SlideSize.Size.Width;
                    var h = (int)pres.SlideSize.Size.Height;
                    foreach (ISlide sld in pres.Slides)
                    {
                        Bitmap bmp = sld.GetThumbnail(new Size(w, h));
                        var index = sld.SlideNumber;
                        Console.WriteLine(index);
                        var f = dir + "\\" + $"{filenameArr[0]}_{index}.png";
                        var serverFile = $"http://10.32.36.174:8099/LView.CMS.Assets/" + $"{courseFile.Type}/{filenameArr[0]}_{index}.png";
                        picList.Add(serverFile);
                        System.IO.File.Delete(f);
                        bmp.Save(f, ImageFormat.Png);
                    }
                }
                ViewData["PicList"] = picList;
            }
            return View();
        }

        public IActionResult ShowVideoDetail(string courseFileId, string courseId, string taskId, string courseRequiredTime)
        {
            if (courseFileId != null)
            {
                var task = MongodbHelper.Instance.FindEntity<LTask>(x => x.Id == taskId);
                var courseProcess = MongodbHelper.Instance.FindEntity<LProcess>(x => x.CourseId == courseId);
                var courseFile = MongodbHelper.Instance.FindEntity<LFile>(x => x.Id == courseFileId);
                var coursePath = $"http://10.32.36.174:8099/LView.CMS.Assets/" + $"{courseFile.Type}/{courseFile.Name}";
                var requiredPeriod = 0;
                task.Category.ForEach(x =>
                {
                    x.Course.ForEach(y =>
                    {
                        if (y.CourseId == courseId)
                        {
                            requiredPeriod = y.CoursePeriod;
                        }
                    });
                });
                var hasGotPeriod = courseProcess.CourseGotPeriod;
                ViewData["RemainTime"] = requiredPeriod * 60 - hasGotPeriod;
                ViewData["VideoUrl"] = coursePath;
            }
            return View();
        }

        public IActionResult ShowExamPageDetail(string courseExamId)
        {
            var exam = MongodbHelper.Instance.FindEntity<LExam>(x => x.Id == courseExamId);
            return View(exam);
        }

        public async Task<string> UpdateProcess(string TaskId, string CourseId, decimal timeOfThisTimes)
        {
            var result = new BaseResult();
            result = await _myTaskService.SaveLProcess(new LProcess
            {
                Id = Guid.NewGuid().ToString(),
                UserId = CurrentUser.UserId,
                TaskId = TaskId,
                CourseId = CourseId
            }, timeOfThisTimes);
            return JsonHelper.ObjectToJSON(result);
        }
    }
}
